import { Component, OnInit, Injector } from '@angular/core';
import { map } from 'rxjs/operators';
import * as marked from 'marked';
import { DetailBase } from 'app/modules/base/detail-base';
import { WebMessage, WebMessageService, WebMessageTo, WebMessageStatus } from '../../services/web-message.service';

@Component({
	selector: 'm-web-message-detail',
	templateUrl: './web-message-detail.component.html',
	styleUrls: ['./web-message-detail.component.scss'],
})
export class WebMessageDetailComponent extends DetailBase implements OnInit {
	constructor(private injector: Injector, private webMessageService: WebMessageService) {
		super(injector);
	}
	WebMessageStatus = WebMessageStatus;
	result$ = this.getData<WebMessageTo>('data.message');
	brother$ = this.getData<{ prev: number; next: number }>('data.brother');
	status = this.route.snapshot.paramMap.get('status');
	html$ = this.result$.pipe(
		map(message_to => {
			const renderer = new marked.Renderer();
			renderer.image = (href, title) => {
				return `<img style="max-width: 100%;" src="/api/${href}" title=${title} />`;
			};
			return marked(message_to.message.content, { renderer });
		}),
	);
	ngOnInit() {}
	async delete() {
		const result = await this.sweet.confirm('确定要删除吗?');
		if (result.dismiss) {
			return;
		}
		const id = Number(this.route.snapshot.queryParamMap.get('id'));
		if (this.status === WebMessageStatus.Sent) {
			await this.webMessageService.sent.delete([id]);
		} else {
			await this.webMessageService.delete([id]);
		}
		this.back();
	}
}
